﻿using System;
using System.Collections.Generic;
using System.Text;
using xFeedTest.DAL;

namespace xFeedTest.BSL
{
    public class objConfigBehaviour
    {
        DBConnection objDB = new DBConnection();
        public System.Data.DataTable getListConfigTableInTime()
        {
            return objDB.getConfigData("", "tblConfig", "ConfigTimeToFeed=DATEPART(hour, getdate()) AND ConfigActive=1");
        }

        public objConfig[] getListConfigObjectInTime()
        {
            List<objConfig> obj = new List<objConfig>();
            System.Data.DataTable tbl = objDB.getConfigData("", "tblConfig", "ConfigTimeToFeed=DATEPART(hour, getdate()) AND ConfigActive=1");
            if(tbl!=null && tbl.Rows.Count>0)
                foreach (System.Data.DataRow dr in tbl.Rows)
                {
                    try
                    {
                        objConfig o = new objConfig();
                        o.ConfigID = (int)dr["ConfigID"];
                        o.ConfigWebsite = dr["ConfigWebsite"].ToString();
                        o.ConfigLinkEx = dr["ConfigLinkEx"].ToString();
                        o.ConfigDataEx = dr["ConfigDataEx"].ToString();
                        o.ConfigTimeToFeed = dr["ConfigTimeToFeed"].ToString();
                        o.ConfigActive = int.Parse(dr["ConfigActive"].ToString());
                        o.ConfigLastUpdate = dr["ConfigLastUpdate"]==null ||dr["ConfigLastUpdate"]== DBNull.Value?null:dr["ConfigLastUpdate"].ToString();
                        o.ConfigCategory = dr["ConfigCategory"].ToString();
                        obj.Add(o);
                    }
                    catch (Exception ex) { }
                }
            return obj.ToArray();
        }

        public objConfig[] getListConfigObject()
        {
            List<objConfig> obj = new List<objConfig>();
            System.Data.DataTable tbl = objDB.getConfigData("", "tblConfig", "ConfigActive=1");
            if (tbl != null && tbl.Rows.Count > 0)
                foreach (System.Data.DataRow dr in tbl.Rows)
                {
                    try
                    {
                        objConfig o = new objConfig();
                        o.ConfigID = (int)dr["ConfigID"];
                        o.ConfigWebsite = dr["ConfigWebsite"].ToString();
                        o.ConfigLinkEx = dr["ConfigLinkEx"].ToString();
                        o.ConfigDataEx = dr["ConfigDataEx"].ToString();
                        o.ConfigTimeToFeed = dr["ConfigTimeToFeed"].ToString();
                        o.ConfigActive = int.Parse(dr["ConfigActive"].ToString());
                        o.ConfigLastUpdate = dr["ConfigLastUpdate"] == null || dr["ConfigLastUpdate"] == DBNull.Value ? null : dr["ConfigLastUpdate"].ToString();
                        o.ConfigCategory = dr["ConfigCategory"].ToString();
                        obj.Add(o);
                    }
                    catch (Exception ex) { }
                }
            return obj.ToArray();
        }
    }
}
